<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="手术信息" >
            <div slot="left" style="display: flex; width: 100%; flex-wrap: wrap; ">
              <el-form-item label="⼿术名称1" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation1Name" placeholder="请输入" clearable />
              </el-form-item>
              <el-form-item label="主⼑医⽣" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation1Doctor" placeholder="请输入" clearable />
              </el-form-item>
              <el-form-item label="⼀助" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation1FirstAssistant" placeholder="请输入" clearable />
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="开始⽇期/时间" label-width="130px">
                <el-date-picker
                  :style="{ width: '240px' }"
                  v-model="models.baqsjRecordSurgicalBad.operation1StartTime"
                  type="datetime"
                  clearable
                  placeholder="选择日期时间"
                />
              </el-form-item>
              <el-form-item label="结束⽇期/时间" label-width="130px">
                <el-date-picker
                  :style="{ width: '240px' }"
                  v-model="models.baqsjRecordSurgicalBad.operation1EndTime"
                  type="datetime"
                  clearable
                  placeholder="选择日期时间"
                />
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="⼿术名称2" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation2Name" placeholder="请输入" clearable />
              </el-form-item>
              <el-form-item label="主⼑医⽣" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation2Doctor" placeholder="请输入" clearable />
              </el-form-item>
              <el-form-item label="⼀助" label-width="130px">
                <el-input v-model="models.baqsjRecordSurgicalBad.operation2FirstAssistant" placeholder="请输入" clearable />
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="开始⽇期/时间" label-width="130px">
                <el-date-picker
                  :style="{ width: '240px' }"
                  v-model="models.baqsjRecordSurgicalBad.operation2StartTime"
                  type="datetime"
                  clearable
                  placeholder="选择日期时间"
                />
              </el-form-item>
              <el-form-item label="结束⽇期/时间" label-width="130px">
                <el-date-picker
                  :style="{ width: '240px' }"
                  v-model="models.baqsjRecordSurgicalBad.operation2EndTime"
                  type="datetime"
                  clearable
                  placeholder="选择日期时间"
                />
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="⼿术类型" prop="surgicalOperationTypeArr" label-width="130px">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.surgicalOperationTypeArr">
                    <el-checkbox v-for="(item, index) in surgicalOperationTypeList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.surgicalOperationTypeArr && models.surgicalOperationTypeArr.indexOf('438')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordSurgicalBad.surgicalOperationTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="⼿术操作是否具有侵⼊性：" prop="isInvasion" label-width="130px">
                <el-radio-group v-model="models.baqsjRecordSurgicalBad.isInvasion">
                  <el-radio v-for="(item, index) in isInvasionList" :key="index" :label="item.value">{{ item.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="错误类型" prop="wrongTypeArr" label-width="130px">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.wrongTypeArr">
                    <el-checkbox v-for="(item, index) in wrongTypeList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.wrongTypeArr && models.wrongTypeArr.indexOf('451')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordSurgicalBad.wrongTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="手术信息">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">⼿术名称1</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation1Name || '' }}</td>
                <td class="td-title">主⼑医⽣</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation1Doctor || '' }}</td>
                <td class="td-title">⼀助</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation1FirstAssistant || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">开始⽇期/时间</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation1StartTime || '' }}</td>
                <td class="td-title">结束⽇期/时间</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation1EndTime || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">⼿术名称2</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation2Name || '' }}</td>
                <td class="td-title">主⼑医⽣</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation2Doctor || '' }}</td>
                <td class="td-title">⼀助</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation2FirstAssistant || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">开始⽇期/时间</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation2StartTime || '' }}</td>
                <td class="td-title">结束⽇期/时间</td>
                <td>{{ models.baqsjRecordSurgicalBad.operation2EndTime || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">⼿术类型</td>
                <td colspan="5">{{ getSurgicalOperationTypes(models.surgicalOperationTypeArr) || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">⼿术操作是否具有侵⼊性</td>
                <td>{{ getIsInvasionLabel(models.baqsjRecordSurgicalBad.isInvasion) || '' }}</td>
                <td class="td-title">错误类型</td>
                <td colspan="3">{{ getWrongTypes(models.wrongTypeArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveSurgicalBad } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureOneInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureOneInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'SurgicalBad',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureOneInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {
        'baqsjRecordSurgicalBad': {
          isInvasion: ''
        }
      },
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      surgicalOperationTypeList: [
        { value: '430', label: '择期⼿术' },
        { value: '431', label: '限期⼿术' },
        { value: '432', label: '急诊⼿术' },
        { value: '433', label: '有创操作' },
        { value: '434', label: '诊断性⼿术' },
        { value: '435', label: '治疗性⼿术' },
        { value: '436', label: '姑息性⼿术' },
        { value: '437', label: '不明' },
        { value: '438', label: '其他' }
      ],
      isInvasionList: [
        { value: 0, label: '是' },
        { value: 1, label: '否' },
        { value: 2, label: '不明' }
      ],
      wrongTypeList: [
        { value: '440', label: '病⼈⾝份确认错误' },
        { value: '441', label: '体检错误' },
        { value: '442', label: '标本未贴标签' },
        { value: '443', label: '标本标签错误' },
        { value: '444', label: '标本遗失' },
        { value: '445', label: '标本收集⽅法不正确' },
        { value: '446', label: '医疗器材不适当' },
        { value: '447', label: '环境设备不合理' },
        { value: '448', label: '未做检查/检验' },
        { value: '449', label: '处置问题' },
        { value: '450', label: '发错检验/检查报告（包括错发、延迟、誊写等）' },
        { value: '451', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        surgicalOperationTypeArr: (info.baqsjRecordSurgicalBad && info.baqsjRecordSurgicalBad.surgicalOperationType) ? info.baqsjRecordSurgicalBad.surgicalOperationType.split(',') : [],
        wrongTypeArr: (info.baqsjRecordSurgicalBad && info.baqsjRecordSurgicalBad.wrongType) ? info.baqsjRecordSurgicalBad.wrongType.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2;
      this.dialogVisible = true;
      this.rules = ruleInfo;
      this.getDetail(info);
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'SurgicalBad',
        eventName: '手术相关-手术不良事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 手术不良 => 对象类型(不同的表单，定制不同)
        baqsjRecordSurgicalBad: {
          isInvasion: ''
        },
        // 事件差错详情 =>  (不同的表单，定制不同)
        wrongTypeArr: [],
        surgicalOperationTypeArr: []
      }
      this.dialogVisible = true
    },
    getSurgicalOperationTypes(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.surgicalOperationTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '438') {
            labels.push(`其他（${this.models.baqsjRecordSurgicalBad.surgicalOperationTypeOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getIsInvasionLabel(value) {
      if (value === undefined || value === null) return '';
      const item = this.isInvasionList.find(item => item.value === value);
      return item ? item.label : '';
    },
    getWrongTypes(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.wrongTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '451') {
            labels.push(`其他（${this.models.baqsjRecordSurgicalBad.wrongTypeOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',');
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',');
          this.models.baqsjRecordSurgicalBad.surgicalOperationType = this.models.surgicalOperationTypeArr.join(',');
          this.models.baqsjRecordSurgicalBad.wrongType = this.models.wrongTypeArr.join(',');
          doSaveSurgicalBad(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
